declare
  mese varchar2(2) :=10;--Mese Lettura mm;A;ALFABETICO^^; 
  anno varchar2(4) :=2012; --^Anno Lettura aaaa;A;ALFABETICO^^;
  com_allaccio number;

  
  
     cursor c_curva_D5 is select decode (ec.cuc_data_curva,null,0,1) cdc,ec.*,codice_allacciamento,
                                    progressivo_utenza,codice_modello_contatore_1,codice_modello_contatore_2--,
                                 -- (select max(TO_NUMBER(SUBSTR(a.cuc_data_curva,1,2)))
                                 -- from DB1.asm_letture_ericsson_curve_v2@pgu3 a
                                 --        where a.cuc_pod=ec.cuc_pod
                                --         and   SUBSTR(a.cuc_data_curva,4,7)=SUBSTR(ec.cuc_data_curva,4,7)) mgg
                                  from DB1.asm_letture_ericsson_curve_v2@pgu3 ec,DB5.utenze 
                                 Where  codice_allacciamento = to_number(substr(ec.cuc_pod(+),7,7))
                                        AND PROGRESSIVO_UTENZA=to_number(substr(ec.cuc_pod(+),14,1))
                                        and substr(cuc_matricola(+),5,9)||'-'||substr(cuc_matricola(+),3,2)=matricola_contatore_1
                                        
                                        and codice_servizio ='E'
                                        and codice_gestione='UNRG'
                                        and substr(ec.CUC_DATA_CURVA(+),4,2)=mese
                                        and substr(ec.CUC_DATA_CURVA(+),7,4)=anno
                                        --and codice_modello_contatore_1 like('M%') 
                                        --and codice_modello_contatore_1 like('%O%')
                                         and codice_modello_contatore_2 IN ('M6AP','M7AP','M8AP','M8OP','M9AP','M9OP') 
                                        --and codice_modello_contatore_2 like('M%') 
                                        --and codice_modello_contatore_2 like('%O%')
                                        --and codice_allacciamento IN (16,12)
                                        and cuc_tipo_cont(+)='TUTELA'
                                        and codice_allacciamento IN(1998815,1999271)
                                        --and  cuc_pod in('IT018E10000551','IT018E10056131','IT018E10054791')--,'IT018E01356131')
                                        order by codice_allacciamento,progressivo_utenza,to_date(ec.cuc_data_curva,'dd/mm/yyyy');
                                        
        
     cursor c_curva_d1 is select decode (ec.cuc_data_curva,null,0,1) cdc,ec.*,codice_allacciamento,
                                    progressivo_utenza,codice_modello_contatore_1,codice_modello_contatore_2--,
                                 -- (select max(TO_NUMBER(SUBSTR(a.cuc_data_curva,1,2)))
                                 -- from DB1.asm_letture_ericsson_curve_v2@pgu3 a
                                 --        where a.cuc_pod=ec.cuc_pod
                                --         and   SUBSTR(a.cuc_data_curva,4,7)=SUBSTR(ec.cuc_data_curva,4,7)) mgg
                                  from DB1.asm_letture_ericsson_curve_v2@pgu3 ec,db1.utenze 
                                 Where  codice_allacciamento = to_number(substr(ec.cuc_pod(+),7,7))
                                        AND PROGRESSIVO_UTENZA=to_number(substr(ec.cuc_pod(+),14,1))
                                        and substr(cuc_matricola(+),5,9)||'-'||substr(cuc_matricola(+),3,2)=matricola_contatore_1
                                        and to_number(substr(cuc_pod(+),7,7)) = codice_allacciamento
                                        and to_number(substr(cuc_pod(+),14,1)) = progressivo_utenza
                                        and codice_servizio ='E'
                                        and codice_gestione='ASM'
                                        and substr(ec.CUC_DATA_CURVA(+),4,2)=mese
                                        and substr(ec.CUC_DATA_CURVA(+),7,4)=anno
                                        --and codice_modello_contatore_1 like('M%') 
                                        --and codice_modello_contatore_1 like('%O%')
                                         and codice_modello_contatore_2 IN ('M6AP','M7AP','M8AP','M8OP','M9AP','M9OP') 
                                        --and codice_modello_contatore_2 like('M%') 
                                        --and codice_modello_contatore_2 like('%O%')
                                       -- and codice_allacciamento IN (16,12)
                                        and cuc_tipo_cont(+)='LIBERI'
                                        and codice_allacciamento IN(1998815,1999271)
                                        --and  cuc_pod in('IT018E10000551','IT018E10056131','IT018E10054791')--,'IT018E01356131')
                                        order by codice_allacciamento,progressivo_utenza,to_date(ec.cuc_data_curva,'dd/mm/yyyy');                                
        
                             
                                     
      type at_ass is Varray(100)   of varchar2(10);  
     aa at_ass;
       type ath_ass is Varray(806)   of varchar2(10);  
     ah ath_ass;
 
      
      rec number;
      
      v_errore number;
     com_curva varchar2(1500);
      k integer;
      v_str_err varchar2(1500);
      TipologiaMisura VARCHAR2(20);
       F1_CONS number;  
       F2_CONS number;    
       F3_CONS number;   
       AdMPtoScTele  VARCHAR2(20);
      pod varchar2(20);CodiceSAPR varchar2(20);MatrContatore_scambio varchar2(20);codice_modello_contatore varchar2(20);PotDispForn number;
      TipoAdM varchar2(20);tensnom VARCHAR2(20);potimpforn number;
     ValoreImmTot number;
     F1_P number;
     F2_P number;
     F3_P number;        ----ENERGIA PRODOTTA CTR PRODUZIONE
                      PRO_CTR_CONS_1 number;     --PRODUZIONE DEL CTR DI CONSUMO 1                      
                      PRO_CTR_CONS_2 number;     --PRODUZIONE DEL CTR DI CONSUMO 2     
                      PRO_CTR_CONS_3 number;    --PRODUZIONE DEL CTR DI CONSUMO 3    
     ValorePrelTot number;
     ac_consumo number;                                  --CONSUMO CONTATORE DI CONSUMO  FASCIA 1
     a_consumo number;                                     -- CONSUMO CONTATORE DI PRODUZIONE FASCIA1
     cc_consumo_c2 number;                                --CONSUMO CONTATORE DI CONSUMO  FASCIA 2
     c_consumo_c2 number;                                   -- CONSUMO CONTATORE DI PRODUZIONE FASCIA2
     cc_consumo_c3 number;                                --CONSUMO CONTATORE DI CONSUMO  FASCIA 3
     c_consumo_c3 number;                                   -- CONSUMO CONTATORE DI PRODUZIONE FASCIA3
     matr_pro varchar2(20);
     modello_contatore_P varchar2(10);
     MAXGG NUMBER;
      mgg number;
     numgg number;
      com_err number;
      
 BEGIN
     com_allaccio:=0;
     aa:= at_ass();
     aa.EXTEND(100);
     ah:= ath_ass();
     ah.EXTEND(806);
     rec:=0;
     IF MESE IN (04 ,06,09) THEN MAXGG:=30; else
     
        IF MESE IN (02) and trunc(anno/3)-(anno/4)= 0  THEN MAXGG:=29; else
            IF MESE IN (02)  THEN MAXGG:=2; else
               MAXGG:=31;
            END IF;
          END IF;
      end if;
   execute immediate 'truncate table ASM_curve_ericsson_ssp';
     execute immediate 'truncate table asm_curve_ericsson_error';
  --begin 
     
      for r_curva IN c_curva_d5 LOOP   
     
   
     BEGIN
               --insert into asm_curve_ericsson_error values (r_curva.cuc_data_curva||'numgg '||numgg||'mgg '||mgg||mese||anno||' '||r_curva.cuc_pod);    
                select max(TO_NUMBER(SUBSTR(a.cuc_data_curva,1,2))),count(distinct substr(a.CUC_DATA_CURVA,1,2)) into mgg,numgg
                                from DB1.asm_letture_ericsson_curve_v2@pgu3 a
                                where a.cuc_pod=r_curva.cuc_pod
                                and substr(a.CUC_DATA_CURVA,4,2)=mese
                                and substr(a.CUC_DATA_CURVA,7,4)=anno
                                and   SUBSTR(a.cuc_data_curva,4,7)=SUBSTR(r_curva.cuc_data_curva,4,7);
               --insert into asm_curve_ericsson_error values (r_curva.cuc_data_curva||'numgg_F '||numgg||'mgg '||mgg||mese||anno||' '||r_curva.cuc_pod);
              if r_curva.codice_allacciamento<> com_allaccio then
                 com_allaccio:=r_curva.codice_allacciamento;
               
                 
                if numgg <> maxgg 
                then
                    if numgg <>0 then
                       v_str_err := 'DB5 Curva non Completa mancano i giorni Allaccio-utenza  = '||
                                          trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza)||' '
                                          ||' Data = '||mese||'/'||anno;
                   insert into asm_curve_ericsson_error values( v_str_err);
                   commit;
                      else 
                    v_str_err := 'DB5 ALLACCIO SENZA CURVA Allaccio-utenza  = '||
                                          trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza)||' '
                                          ||' Data = '||mese||'/'||anno;
                    insert into asm_curve_ericsson_error values( v_str_err);
                      commit;
                    end if;
                  end if;
             end if;
                  
     end;
     
     
     
    -- begin
     
     
     -----CURVE PER IMPIANTI IN CONFIGURAZIONE C
     --    select ec.cuc_curvav4 into com_curva
      --                            from db1.asm_letture_ericsson_curve_v2@pgu3 ec,DB5.utenze 
      --                           Where  codice_allacciamento = to_number(substr(ec.cuc_pod(+),7,7))
      --                                  AND PROGRESSIVO_UTENZA=to_number(substr(ec.cuc_pod(+),14,1))
      --                                 and substr(cuc_matricola,5,9)||'-'||substr(cuc_matricola,3,2)=matricola_contatore_2
      --                                  and codice_servizio ='E'
      --                                  and codice_gestione='UNRG'
       --                                 and codice_modello_contatore_2 in('M8OP') 
      --                                 --and  codice_allacciamento IN (11,100,1048,1059,1131,1005613,1005479,135613,1999977,1999844)
       --                                 --and cuc_tipo_cont='TUTELA'
       --                                 and r_curva.cuc_pod=ec.cuc_pod
       --                                 and substr(ec.CUC_DATA_CURVA,4,2)=mese
      --                                  and substr(ec.CUC_DATA_CURVA,7,4)=anno
      --                                  and r_curva.cuc_data_curva= ec.cuc_data_curva;
       --        EXCEPTION
                 
       
                      
        --              WHEN TOO_MANY_ROWS THEN
                       
       --                v_str_err := ' DB5 too many row: curve schema C ' ;
          --             v_errore := 1;
        --             GOTO fine_LOOP_db5;  
                     
                                      
        --         WHEN NO_DATA_FOUND THEN
          --            com_curva :=r_curva.cuc_curvav4;
                     
                     
     -- END;
  
    for i IN aa.FIRST..aa.LAST LOOP 
           com_err:=instr(com_curva,'-1');
           com_curva :=replace (com_curva,'-1','0');
        if r_curva.cdc<>0 then
         -- ELABORA CURVA ***********************************
             -- CONTROLLO SE CURVA NON COPLETA ******************
        
             if (length(trim(com_curva))-length(trim(replace(com_curva,';',null))) <97 or com_err <> 0) and i=1 then
                   v_str_err := 'DB5 Curva non Completa POD = '||
                                          trim(r_curva.cuc_pod)||'-'
                                          ||' Data = '||r_curva.cuc_data_curva;
                   insert into asm_curve_ericsson_error values( v_str_err);
             end if;
        commit;
           
        
           -----  ESTRAI CONSUMI PER QUARTO D'ORA E SOMMA per ORA **************
        
        
        aa(i):=to_number(substr(com_curva,instr(com_curva,';',1,i)+1,(instr(com_curva,';',1,i+1)-(instr(com_curva,';',1,i))-1)));
        REC:=(TO_NUMBER(SUBSTR(r_curva.cuc_data_curva,1,2))-1)*26;
        k:=trunc(i/4+0.9)+1+rec;
        ah(rec+1):=r_curva.cuc_data_curva;
        ah(K) := +nvl(aa(i),0)+nvl(ah(k),0);
       -- insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-1;'||k||'rec-;'||rec||'I-;'||i);
        end if;
        
         --  insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-2;'||k||'rec-;'||rec||'I-;'||i);
           commit;
           
          if   (k=MGG*26 and i=100) or r_curva.cdc=0 then
          --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-3;'||k||'rec-;'||rec||'I-;'||i);
          commit;
    
                 BEGIN
    
                    
                  select 
                CONCAT('IT018E',concat(substr(concat('00000000',UT.CODICE_ALLACCIAMENTO),-7,7),substr(UT.PROGRESSIVO_UTENZA,-1,1))),
                CONCAT('S-',concat('IT018E',concat(substr(concat('00000000',UT.CODICE_ALLACCIAMENTO),-7,7),substr(UT.PROGRESSIVO_UTENZA,-1,1)))),
                ac.MATRICOLA_CONTATORE,
                 ac.codice_modello_contatore,
       
                co.POTENZA_TOLLERATA ,
            
                'E',
                UT.tensione_prevista ,
                co.POTENZA_IMPEGNATA ,
                null,
                 c.consumo_p1,c.consumo_p2,c.consumo_p3,              ---------- energia prodotta contatore di produzione     
                 CC.CONSUMO_P1 PRO_CTR_CONS_1,                            
                  CC.CONSUMO_P2 PRO_CTR_CONS_2,     
                  CC.CONSUMO_P3 PRO_CTR_CONS_3,                        
                                             
                                             
                null,
                     ac.consumo,   --CONSUMO CONTATORE DI CONSUMO  FASCIA 1
                     a.consumo cons_prod_cons_c1, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA1
                     cc.consumo_c2,  --CONSUMO CONTATORE DI CONSUMO  FASCIA 2
                     c.consumo_c2 cons_prod_cons_c2, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA2
                     cc.consumo_c3,  --CONSUMO CONTATORE DI CONSUMO  FASCIA 3
                     c.consumo_c3 cons_prod_cons_c3, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA3
                     
                     
                     
               
                a.matricola_contatore,
                a.codice_modello_contatore
               
                into pod,CodiceSAPR,MatrContatore_scambio,codice_modello_contatore,PotDispForn,
                TipoAdM,tensnom,potimpforn,
                     ValoreImmTot,f1_p,f2_p,f3_p,
                      PRO_CTR_CONS_1,      --PRODUZIONE DEL CTR DI CONSUMO 1                      
                      PRO_CTR_CONS_2,      --PRODUZIONE DEL CTR DI CONSUMO 2     
                      PRO_CTR_CONS_3,        --PRODUZIONE DEL CTR DI CONSUMO 3    
                     
                     ValorePrelTot,
                     ac_consumo,a_consumo,cc_consumo_c2,c_consumo_c2,cc_consumo_c3,c_consumo_c3,
                     
                     matr_pro,modello_contatore_P
               from DB5.UTENZE UT,DB5.contatori_movimenti a,

                
                DB5.contatori_movimenti_ls c,
                DB5.contatori_movimenti_ls cc,DB5.contatori_movimenti ac,DB5.contratti co

                where 
                
                UT.codice_allacciamento= r_curva.codice_allacciamento
                AND UT.PROGRESSIVO_UTENZA=r_curva.PROGRESSIVO_UTENZA    
                and a.codice_servizio(+) ='E'
                and c.codice_servizio(+) ='E'
                and a.codice_modello_contatore(+)=UT.codice_modello_contatore_2
                and a.matricola_contatore(+)=UT.matricola_contatore_2             
               and a.codice_modello_contatore=c.codice_modello_contatore(+)
               and a.matricola_contatore=c.matricola_contatore(+)
               and a.progressivo_movimento=c.progressivo_movimento(+)
               and (a.progressivo_movimento = (select max(Progressivo_movimento) from DB5.contatori_movimenti x
                                               where x.codice_servizio='E'
                                                and to_char(data_movimento,'mm')= mese
                                               and to_char(data_movimento,'yyyy')= anno
                                               and a.matricola_contatore=x.matricola_contatore
                                               and a.codice_modello_contatore=x.codice_modello_contatore)
                                               OR a.progressivo_movimento IS NULL)
                --and to_char(a.data_movimento(+),'mm')= '04'
                --and to_char(a.data_movimento(+),'yyyy')= '2012'                               
                and ac.codice_modello_contatore=UT.codice_modello_contatore_1 
                and ac.matricola_contatore=UT.matricola_contatore_1                             
                and ac.codice_servizio ='E'
                and cc.codice_servizio(+) ='E'
                and ac.codice_modello_contatore=cc.codice_modello_contatore(+)
                and ac.matricola_contatore=cc.matricola_contatore(+)
                and ac.progressivo_movimento=cc.progressivo_movimento(+)            
                and ac.progressivo_movimento = (select max(Progressivo_movimento) from DB5.contatori_movimenti x
                                               where x.codice_servizio ='E'
                                               and to_char(data_movimento,'mm')= mese
                                               and to_char(data_movimento,'yyyy')= anno
                                               and ac.matricola_contatore=x.matricola_contatore
                                               and ac.codice_modello_contatore=x.codice_modello_contatore)                              
                --and to_char(ac.data_movimento,'mm')= '05'
                --and to_char(ac.data_movimento,'yyyy')= '2012'
                and ac.codice_gestione=UT.codice_gestione
                and co.codice_gestione=UT.codice_gestione
                and co.codice_contratto=UT.codice_contratto_CORR
                AND CO.CODICE_SERVIZIO=UT.CODICE_SERVIZIO;
               
                
                
                EXCEPTION
                
                 WHEN NO_DATA_FOUND THEN
          
                     
                      insert into asm_curve_ericsson_error values( 'DB5 POD non letto (FIM) il '||mese||'/'||anno|| ' Allaccio-utenza = '||
                                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza));
                     
                      ah:= ath_ass();
                      ah.EXTEND(806);
                      COMMIT;
                      GOTO FINE_REC_DB5;
                   
                      
                      WHEN TOO_MANY_ROWS THEN
                       
                       v_str_err := 'DB5 too many row: ' ;
                       v_errore := 1;
                     GOTO fine_LOOP_db5; 
                --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-4;'||k||'rec-;'||rec||'I-;'||i); 
                commit;         
              end;
          --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-5;'||k||'rec-;'||rec||'I-;'||i);        
            commit;
    
           --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-6;'||k||'rec-;'||rec||'I-;'||i);
               commit;
    
             if
               codice_modello_contatore = 'M3OM' then TipologiaMisura:='O'; else 
             if codice_modello_contatore in('M2AT','M3AC','M3AR','M3AM') then TipologiaMisura:='F'; else TipologiaMisura:='M';
             end if;
           end  if;
    
    
             if codice_modello_contatore  in('M2AT','M3AC','M3AR','M3AM','M3OM') then AdMPtoScTele :='Y'; else AdMPtoScTele :='N';
             end if; 
    
   
   
                        if modello_contatore_p like 'M6%' or  modello_contatore_p like 'M8%'
                             then F1_CONS:=nvl(Ac_consumo,0)+nvl(A_CONSUMO,0)-NVL(F1_p,0); 
                                        F2_CONS:=nvl(Cc_consumo_C2,0)+nvl(C_CONSUMO_C2,0)-NVL(F2_p,0);
                                        F3_CONS:=nvl(Cc_consumo_C3,0)+nvl(C_CONSUMO_C3,0)-NVL(F3_p,0); 
                                         F1_P:=F1_CONS*-1;
                                         F2_P:=F2_CONS*-1;
                                         F3_P:=F3_CONS*-1;
                                        
                             else 
                                        F1_CONS:=nvl(Ac_consumo,0); 
                                        F2_CONS:=nvl(Cc_consumo_C2,0);
                                        F3_CONS:=nvl(Cc_consumo_C3,0); 
                                        F1_P:=NVL(PRO_CTR_CONS_1,0);
                                        F2_P:=NVL(PRO_CTR_CONS_2,0);
                                        F3_P:=NVL(PRO_CTR_CONS_3,0);
                                        
                        end if;
                        
              
                                                               if f1_p < 0 then f1_p:=0; end if;
                                                               if f2_p < 0 then f2_p:=0; end if;
                                                               if f3_p < 0 then f3_p:=0; end if;
                                                               if f1_CONS < 0 then f1_CONS:=0; end if;
                                                               if f2_CONS < 0 then f2_CONS:=0; end if;
                                                               if f3_CONS < 0 then f3_CONS:=0; end if;
                                                                              
              IF   tensnom = 'B' THEN   TENSNOM :=220; ELSE  TENSNOM :=380;  END IF;                
                            
   
    
               insert into ASM_curve_ericsson_ssp values (
  
               '018',
              '05',
              '2012',
              pod,
              CodiceSAPR,
              MatrContatore_scambio ,
              TipologiaMisura,
      
              PotDispForn,
              AdMPtoScTele ,
              TipoAdM,
              TO_NUMBER(tensnom),
              potimpforn,
             ValoreImmTot,
             F1_P,
             
             F2_P,
             F3_P,
             0,
             ValorePrelTot,
             F1_CONS,
             F2_CONS,
             F3_CONS,
             0,
            ah(1),ah(2),ah(3),ah(4),ah(5),ah(6),ah(7),ah(8),ah(9),ah(10),ah(11),ah(12),ah(13),ah(14),ah(15),ah(16),ah(17),ah(18),ah(19),ah(20),ah(21),ah(22),ah(23),ah(24),ah(25),ah(26),
            ah(27),ah(28),ah(29),ah(30),ah(31),ah(32),ah(33),ah(34),ah(35),ah(36),ah(37),ah(38),ah(39),ah(40),ah(41),ah(42),ah(43),ah(44),ah(45),ah(46),ah(47),ah(48),ah(49),ah(50),ah(51),ah(52),
            ah(53),ah(54),ah(55),ah(56),ah(57),ah(58),ah(59),ah(60),ah(61),ah(62),ah(63),ah(64),ah(65),ah(66),ah(67),ah(68),ah(69),ah(70),ah(71),ah(72),ah(73),ah(74),ah(75),ah(76),ah(77),ah(78),
            ah(79),ah(80),ah(81),ah(82),ah(83),ah(84),ah(85),ah(86),ah(87),ah(88),ah(89),ah(90),ah(91),ah(92),ah(93),ah(94),ah(95),ah(96),ah(97),ah(98),ah(99),ah(100),ah(101),ah(102),ah(103),ah(104),
            ah(105),ah(106),ah(107),ah(108),ah(109),ah(110),ah(111),ah(112),ah(113),ah(114),ah(115),ah(116),ah(117),ah(118),ah(119),ah(120),ah(121),ah(122),ah(123),ah(124),ah(125),ah(126),ah(127),ah(128),ah(129),ah(130),
            ah(131),ah(132),ah(133),ah(134),ah(135),ah(136),ah(137),ah(138),ah(139),ah(140),ah(141),ah(142),ah(143),ah(144),ah(145),ah(146),ah(147),ah(148),ah(149),ah(150),ah(151),ah(152),ah(153),ah(154),ah(155),ah(156),
            ah(157),ah(158),ah(159),ah(160),ah(161),ah(162),ah(163),ah(164),ah(165),ah(166),ah(167),ah(168),ah(169),ah(170),ah(171),ah(172),ah(173),ah(174),ah(175),ah(176),ah(177),ah(178),ah(179),ah(180),ah(181),ah(182),
            ah(183),ah(184),ah(185),ah(186),ah(187),ah(188),ah(189),ah(190),ah(191),ah(192),ah(193),ah(194),ah(195),ah(196),ah(197),ah(198),ah(199),ah(200),ah(201),ah(202),ah(203),ah(204),ah(205),ah(206),ah(207),ah(208),
            ah(209),ah(210),ah(211),ah(212),ah(213),ah(214),ah(215),ah(216),ah(217),ah(218),ah(219),ah(220),ah(221),ah(222),ah(223),ah(224),ah(225),ah(226),ah(227),ah(228),ah(229),ah(230),ah(231),ah(232),ah(233),ah(234),
            ah(235),ah(236),ah(237),ah(238),ah(239),ah(240),ah(241),ah(242),ah(243),ah(244),ah(245),ah(246),ah(247),ah(248),ah(249),ah(250),ah(251),ah(252),ah(253),ah(254),ah(255),ah(256),ah(257),ah(258),ah(259),ah(260),
            ah(261),ah(262),ah(263),ah(264),ah(265),ah(266),ah(267),ah(268),ah(269),ah(270),ah(271),ah(272),ah(273),ah(274),ah(275),ah(276),ah(277),ah(278),ah(279),ah(280),ah(281),ah(282),ah(283),ah(284),ah(285),ah(286),
            ah(287),ah(288),ah(289),ah(290),ah(291),ah(292),ah(293),ah(294),ah(295),ah(296),ah(297),ah(298),ah(299),ah(300),ah(301),ah(302),ah(303),ah(304),ah(305),ah(306),ah(307),ah(308),ah(309),ah(310),ah(311),ah(312),
            ah(313),ah(314),ah(315),ah(316),ah(317),ah(318),ah(319),ah(320),ah(321),ah(322),ah(323),ah(324),ah(325),ah(326),ah(327),ah(328),ah(329),ah(330),ah(331),ah(332),ah(333),ah(334),ah(335),ah(336),ah(337),ah(338),
            ah(339),ah(340),ah(341),ah(342),ah(343),ah(344),ah(345),ah(346),ah(347),ah(348),ah(349),ah(350),ah(351),ah(352),ah(353),ah(354),ah(355),ah(356),ah(357),ah(358),ah(359),ah(360),ah(361),ah(362),ah(363),ah(364),
            ah(365),ah(366),ah(367),ah(368),ah(369),ah(370),ah(371),ah(372),ah(373),ah(374),ah(375),ah(376),ah(377),ah(378),ah(379),ah(380),ah(381),ah(382),ah(383),ah(384),ah(385),ah(386),ah(387),ah(388),ah(389),ah(390),
            ah(391),ah(392),ah(393),ah(394),ah(395),ah(396),ah(397),ah(398),ah(399),ah(400),ah(401),ah(402),ah(403),ah(404),ah(405),ah(406),ah(407),ah(408),ah(409),ah(410),ah(411),ah(412),ah(413),ah(414),ah(415),ah(416),
            ah(417),ah(418),ah(419),ah(420),ah(421),ah(422),ah(423),ah(424),ah(425),ah(426),ah(427),ah(428),ah(429),ah(430),ah(431),ah(432),ah(433),ah(434),ah(435),ah(436),ah(437),ah(438),ah(439),ah(440),ah(441),ah(442),
            ah(443),ah(444),ah(445),ah(446),ah(447),ah(448),ah(449),ah(450),ah(451),ah(452),ah(453),ah(454),ah(455),ah(456),ah(457),ah(458),ah(459),ah(460),ah(461),ah(462),ah(463),ah(464),ah(465),ah(466),ah(467),ah(468),
            ah(469),ah(470),ah(471),ah(472),ah(473),ah(474),ah(475),ah(476),ah(477),ah(478),ah(479),ah(480),ah(481),ah(482),ah(483),ah(484),ah(485),ah(486),ah(487),ah(488),ah(489),ah(490),ah(491),ah(492),ah(493),ah(494),
            ah(495),ah(496),ah(497),ah(498),ah(499),ah(500),ah(501),ah(502),ah(503),ah(504),ah(505),ah(506),ah(507),ah(508),ah(509),ah(510),ah(511),ah(512),ah(513),ah(514),ah(515),ah(516),ah(517),ah(518),ah(519),ah(520),
            ah(521),ah(522),ah(523),ah(524),ah(525),ah(526),ah(527),ah(528),ah(529),ah(530),ah(531),ah(532),ah(533),ah(534),ah(535),ah(536),ah(537),ah(538),ah(539),ah(540),ah(541),ah(542),ah(543),ah(544),ah(545),ah(546),
            ah(547),ah(548),ah(549),ah(550),ah(551),ah(552),ah(553),ah(554),ah(555),ah(556),ah(557),ah(558),ah(559),ah(560),ah(561),ah(562),ah(563),ah(564),ah(565),ah(566),ah(567),ah(568),ah(569),ah(570),ah(571),ah(572),
            ah(573),ah(574),ah(575),ah(576),ah(577),ah(578),ah(579),ah(580),ah(581),ah(582),ah(583),ah(584),ah(585),ah(586),ah(587),ah(588),ah(589),ah(590),ah(591),ah(592),ah(593),ah(594),ah(595),ah(596),ah(597),ah(598),
            ah(599),ah(600),ah(601),ah(602),ah(603),ah(604),ah(605),ah(606),ah(607),ah(608),ah(609),ah(610),ah(611),ah(612),ah(613),ah(614),ah(615),ah(616),ah(617),ah(618),ah(619),ah(620),ah(621),ah(622),ah(623),ah(624),
            ah(625),ah(626),ah(627),ah(628),ah(629),ah(630),ah(631),ah(632),ah(633),ah(634),ah(635),ah(636),ah(637),ah(638),ah(639),ah(640),ah(641),ah(642),ah(643),ah(644),ah(645),ah(646),ah(647),ah(648),ah(649),ah(650),
            ah(651),ah(652),ah(653),ah(654),ah(655),ah(656),ah(657),ah(658),ah(659),ah(660),ah(661),ah(662),ah(663),ah(664),ah(665),ah(666),ah(667),ah(668),ah(669),ah(670),ah(671),ah(672),ah(673),ah(674),ah(675),ah(676),
            ah(677),ah(678),ah(679),ah(680),ah(681),ah(682),ah(683),ah(684),ah(685),ah(686),ah(687),ah(688),ah(689),ah(690),ah(691),ah(692),ah(693),ah(694),ah(695),ah(696),ah(697),ah(698),ah(699),ah(700),ah(701),ah(702),
            ah(703),ah(704),ah(705),ah(706),ah(707),ah(708),ah(709),ah(710),ah(711),ah(712),ah(713),ah(714),ah(715),ah(716),ah(717),ah(718),ah(719),ah(720),ah(721),ah(722),ah(723),ah(724),ah(725),ah(726),ah(727),ah(728),
            ah(729),ah(730),ah(731),ah(732),ah(733),ah(734),ah(735),ah(736),ah(737),ah(738),ah(739),ah(740),ah(741),ah(742),ah(743),ah(744),ah(745),ah(746),ah(747),ah(748),ah(749),ah(750),ah(751),ah(752),ah(753),ah(754),
            ah(755),ah(756),ah(757),ah(758),ah(759),ah(760),ah(761),ah(762),ah(763),ah(764),ah(765),ah(766),ah(767),ah(768),ah(769),ah(770),ah(771),ah(772),ah(773),ah(774),ah(775),ah(776),ah(777),ah(778),ah(779),ah(780),
            ah(781),ah(782),ah(783),ah(784),ah(785),ah(786),ah(787),ah(788),ah(789),ah(790),ah(791),ah(792),ah(793),ah(794),ah(795),ah(796),ah(797),ah(798),ah(799),ah(800),ah(801),ah(802),ah(803),ah(804),ah(805),ah(806),
            matr_pro );
            
            ah:= ath_ass();
            ah.EXTEND(806);
         end if;
         --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-6;'||k||'rec-;'||rec||'I-;'||i);
        commit;
        if r_curva.cdc = 0 then 
        
        v_str_err := 'DB5 POD SENZA CURVA Allaccio-utenza = '||
                                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza);
                      insert into asm_curve_ericsson_error values( v_str_err);
        --end if;
        --if length(trim(r_curva.cuc_curvav1))-length(trim(replace(r_curva.cuc_curvav1,';',null))) <97 then
        --v_str_err := 'Curva non Completa Allaccio-utenza = '||
                --                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza);
                --      insert into asm_curve_ericsson_error values( v_str_err);
        commit;
        goto fine_rec_DB5; 
        end if;
        
        
       
      
    end loop;  
    <<FINE_REC_DB5>>
      
       
    commit;
   end loop;
     <<fine_loop_db5>> 
    COMMIT;
     if v_errore = 1
     then 
     
       insert into asm_curve_ericsson_error values( v_str_err);
       COMMIT;
       v_errore:=0;
     end if; 
      commit;
     -----------------**************************************************************** end;
     -----------------**************************************************************** end;
     -----------------**************************************************************** end;
     -----------------**************************************************************** end;
     -----------------**************************************************************** end;
 ----------------------------------------BEGIN
     com_allaccio:=0;
     aa:= at_ass();
     aa.EXTEND(100);
     ah:= ath_ass();
     ah.EXTEND(806);
     rec:=0;

  
     
      for r_curva IN c_curva_d1 LOOP   
     
   
     BEGIN
                   
                select max(TO_NUMBER(SUBSTR(a.cuc_data_curva,1,2))),count(distinct substr(a.CUC_DATA_CURVA,1,2)) into mgg,numgg
                                from DB1.asm_letture_ericsson_curve_v2@pgu3 a
                                where a.cuc_pod=r_curva.cuc_pod
                                and substr(a.CUC_DATA_CURVA,4,2)=mese
                                and substr(a.CUC_DATA_CURVA,7,4)=anno
                                and   SUBSTR(a.cuc_data_curva,4,7)=SUBSTR(r_curva.cuc_data_curva,4,7);
              if r_curva.codice_allacciamento<> com_allaccio then
                 com_allaccio:=r_curva.codice_allacciamento;
                if numgg <> maxgg 
                then
                    if numgg <>0 then
                       v_str_err := 'db1 Curva non Completa mancano i giorni Allaccio-utenza  = '||
                                          trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza)||' '
                                          ||' Data = '||mese||'/'||anno;
                   insert into asm_curve_ericsson_error values( v_str_err);
                   commit;
                      else 
                    v_str_err := 'db1 ALLACCIO SENZA CURVA Allaccio-utenza  = '||
                                          trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza)||' '
                                          ||' Data = '||mese||'/'||anno;
                    insert into asm_curve_ericsson_error values( v_str_err);
                      commit;
                    end if;
                  end if;
             end if;
                  
     end;
     
     
     
    
    for i IN aa.FIRST..aa.LAST LOOP 
    com_err:=instr(com_curva,'-1');
       com_curva :=replace (com_curva,'-1','0');    
        if r_curva.cdc<>0 then
         -- ELABORA CURVA ***********************************
             -- CONTROLLO SE CURVA NON COPLETA ******************
        
             if (length(trim(com_curva))-length(trim(replace(com_curva,';',null))) <97 or com_err <> 0) and i=1 then
                   v_str_err := 'db1 Curva non Completa POD = '||
                                          trim(r_curva.cuc_pod)||'-'
                                          ||' Data = '||r_curva.cuc_data_curva;
                   insert into asm_curve_ericsson_error values( v_str_err);
             end if;
        commit;
           
        
           -----  ESTRAI CONSUMI PER QUARTO D'ORA E SOMMA per ORA **************
        
        
        aa(i):=to_number(substr(com_curva,instr(com_curva,';',1,i)+1,(instr(com_curva,';',1,i+1)-(instr(com_curva,';',1,i))-1)));
        REC:=(TO_NUMBER(SUBSTR(r_curva.cuc_data_curva,1,2))-1)*26;
        k:=trunc(i/4+0.9)+1+rec;
        ah(rec+1):=r_curva.cuc_data_curva;
        ah(K) := +nvl(aa(i),0)+nvl(ah(k),0);
       -- insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-1;'||k||'rec-;'||rec||'I-;'||i);
        end if;
        
         --  insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-2;'||k||'rec-;'||rec||'I-;'||i);
           commit;
           
          if   (k=MGG*26 and i=100) or r_curva.cdc=0 then
          --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-3;'||k||'rec-;'||rec||'I-;'||i);
          commit;
    
                 BEGIN
    
                    
                
                  select 
                CONCAT('IT018E',concat(substr(concat('00000000',UT.CODICE_ALLACCIAMENTO),-7,7),substr(UT.PROGRESSIVO_UTENZA,-1,1))),
                CONCAT('S-',concat('IT018E',concat(substr(concat('00000000',UT.CODICE_ALLACCIAMENTO),-7,7),substr(UT.PROGRESSIVO_UTENZA,-1,1)))),
                ac.MATRICOLA_CONTATORE,
                 ac.codice_modello_contatore,
       
                co.POTENZA_TOLLERATA ,
            
                'E',
                UT.tensione_prevista ,
                co.POTENZA_IMPEGNATA ,
                null,
                 c.consumo_p1,c.consumo_p2,c.consumo_p3,              ---------- energia prodotta contatore di produzione     
                 CC.CONSUMO_P1 PRO_CTR_CONS_1,                            
                  CC.CONSUMO_P2 PRO_CTR_CONS_2,     
                  CC.CONSUMO_P3 PRO_CTR_CONS_3,                        
                                             
                                             
                null,
                     ac.consumo,   --CONSUMO CONTATORE DI CONSUMO  FASCIA 1
                     a.consumo cons_prod_cons_c1, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA1
                     cc.consumo_c2,  --CONSUMO CONTATORE DI CONSUMO  FASCIA 2
                     c.consumo_c2 cons_prod_cons_c2, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA2
                     cc.consumo_c3,  --CONSUMO CONTATORE DI CONSUMO  FASCIA 3
                     c.consumo_c3 cons_prod_cons_c3, -- CONSUMO CONTATORE DI PRODUZIONE FASCIA3
                     
                     
                     
               
                a.matricola_contatore,
                a.codice_modello_contatore
               
                into pod,CodiceSAPR,MatrContatore_scambio,codice_modello_contatore,PotDispForn,
                TipoAdM,tensnom,potimpforn,
                     ValoreImmTot,f1_p,f2_p,f3_p,
                      PRO_CTR_CONS_1,      --PRODUZIONE DEL CTR DI CONSUMO 1                      
                      PRO_CTR_CONS_2,      --PRODUZIONE DEL CTR DI CONSUMO 2     
                      PRO_CTR_CONS_3,        --PRODUZIONE DEL CTR DI CONSUMO 3    
                     
                     ValorePrelTot,
                     ac_consumo,a_consumo,cc_consumo_c2,c_consumo_c2,cc_consumo_c3,c_consumo_c3,
                     
                     matr_pro,modello_contatore_P
               from db1.UTENZE UT,db1.contatori_movimenti a,

                
                db1.contatori_movimenti_ls c,
                db1.contatori_movimenti_ls cc,db1.contatori_movimenti ac,db1.contratti co

                where 
                
                UT.codice_allacciamento= r_curva.codice_allacciamento
                AND UT.PROGRESSIVO_UTENZA=r_curva.PROGRESSIVO_UTENZA    
                and a.codice_servizio(+) ='E'
                and c.codice_servizio(+) ='E'
                and a.codice_modello_contatore(+)=UT.codice_modello_contatore_2
                and a.matricola_contatore(+)=UT.matricola_contatore_2             
               and a.codice_modello_contatore=c.codice_modello_contatore(+)
               and a.matricola_contatore=c.matricola_contatore(+)
               and a.progressivo_movimento=c.progressivo_movimento(+)
               and (a.progressivo_movimento = (select max(Progressivo_movimento) from db1.contatori_movimenti x
                                               where x.codice_servizio='E'
                                                and to_char(data_movimento,'mm')= mese
                                               and to_char(data_movimento,'yyyy')= anno
                                               and a.matricola_contatore=x.matricola_contatore
                                               and a.codice_modello_contatore=x.codice_modello_contatore)
                                               OR a.progressivo_movimento IS NULL)
                --and to_char(a.data_movimento(+),'mm')= '04'
                --and to_char(a.data_movimento(+),'yyyy')= '2012'                               
                and ac.codice_modello_contatore=UT.codice_modello_contatore_1 
                and ac.matricola_contatore=UT.matricola_contatore_1                             
                and ac.codice_servizio ='E'
                and cc.codice_servizio(+) ='E'
                and ac.codice_modello_contatore=cc.codice_modello_contatore(+)
                and ac.matricola_contatore=cc.matricola_contatore(+)
                and ac.progressivo_movimento=cc.progressivo_movimento(+)            
                and ac.progressivo_movimento = (select max(Progressivo_movimento) from db1.contatori_movimenti x
                                               where x.codice_servizio ='E'
                                               and to_char(data_movimento,'mm')= mese
                                               and to_char(data_movimento,'yyyy')= anno
                                               and ac.matricola_contatore=x.matricola_contatore
                                               and ac.codice_modello_contatore=x.codice_modello_contatore)                              
                --and to_char(ac.data_movimento,'mm')= '05'
                --and to_char(ac.data_movimento,'yyyy')= '2012'
                and ac.codice_gestione=UT.codice_gestione
                and co.codice_gestione=UT.codice_gestione
                and co.codice_contratto=UT.codice_contratto_CORR
                AND CO.CODICE_SERVIZIO=UT.CODICE_SERVIZIO;
               
                
                
                EXCEPTION
                
                 WHEN NO_DATA_FOUND THEN
          
                     
                      insert into asm_curve_ericsson_error values( 'db1 POD non letto (FIMM) il '||mese||'/'||anno|| ' Allaccio-utenza = '||
                                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza));
                     
                      ah:= ath_ass();
                      ah.EXTEND(806);
                      COMMIT;
                      GOTO FINE_REC_db1;
                   
                      
                      WHEN TOO_MANY_ROWS THEN
                       
                       v_str_err := 'db1 too many row: ' ;
                       v_errore := 1;
                     GOTO fine_LOOP_db1; 
                --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-4;'||k||'rec-;'||rec||'I-;'||i); 
                commit;         
              end;
          --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-5;'||k||'rec-;'||rec||'I-;'||i);        
            commit;
    
           --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-6;'||k||'rec-;'||rec||'I-;'||i);
               commit;
    
             if
               codice_modello_contatore = 'M3OM' then TipologiaMisura:='O'; else 
             if codice_modello_contatore in('M2AT','M3AC','M3AR','M3AM') then TipologiaMisura:='F'; else TipologiaMisura:='M';
             end if;
           end  if;
    
    
             if codice_modello_contatore  in('M2AT','M3AC','M3AR','M3AM','M3OM') then AdMPtoScTele :='Y'; else AdMPtoScTele :='N';
             end if; 
    
   
      
                        if modello_contatore_p like 'M6%' or  modello_contatore_p like 'M8%'
                             then F1_CONS:=nvl(Ac_consumo,0)+nvl(A_CONSUMO,0)-NVL(F1_p,0); 
                                        F2_CONS:=nvl(Cc_consumo_C2,0)+nvl(C_CONSUMO_C2,0)-NVL(F2_p,0);
                                        F3_CONS:=nvl(Cc_consumo_C3,0)+nvl(C_CONSUMO_C3,0)-NVL(F3_p,0); 
                                         F1_P:=F1_CONS*-1;
                                         F2_P:=F2_CONS*-1;
                                         F3_P:=F3_CONS*-1;
                                        
                             else 
                                        F1_CONS:=nvl(Ac_consumo,0); 
                                        F2_CONS:=nvl(Cc_consumo_C2,0);
                                        F3_CONS:=nvl(Cc_consumo_C3,0); 
                                        F1_P:=NVL(PRO_CTR_CONS_1,0);
                                        F2_P:=NVL(PRO_CTR_CONS_2,0);
                                         F3_P:=NVL(PRO_CTR_CONS_3,0);
                                        
                        end if;
                        
              
                                                               if f1_p < 0 then f1_p:=0; end if;
                                                               if f2_p < 0 then f2_p:=0; end if;
                                                               if f3_p < 0 then f3_p:=0; end if;
                                                               if f1_CONS < 0 then f1_CONS:=0; end if;
                                                               if f2_CONS < 0 then f2_CONS:=0; end if;
                                                               if f3_CONS < 0 then f3_CONS:=0; end if;
                                  
              IF   tensnom = 'B' THEN   TENSNOM :=220; ELSE  TENSNOM :=380;  END IF;                
                            
   
    
               insert into ASM_curve_ericsson_ssp values (
  
               '018',
              '05',
              '2012',
              pod,
              CodiceSAPR,
              MatrContatore_scambio ,
              TipologiaMisura,
      
              PotDispForn,
              AdMPtoScTele ,
              TipoAdM,
              TO_NUMBER(tensnom),
              potimpforn,
             ValoreImmTot,
             F1_P,F2_P,F3_P,0,
             ValorePrelTot,
             F1_CONS,
             F2_CONS,
             F3_CONS,
             0,
            ah(1),ah(2),ah(3),ah(4),ah(5),ah(6),ah(7),ah(8),ah(9),ah(10),ah(11),ah(12),ah(13),ah(14),ah(15),ah(16),ah(17),ah(18),ah(19),ah(20),ah(21),ah(22),ah(23),ah(24),ah(25),ah(26),
            ah(27),ah(28),ah(29),ah(30),ah(31),ah(32),ah(33),ah(34),ah(35),ah(36),ah(37),ah(38),ah(39),ah(40),ah(41),ah(42),ah(43),ah(44),ah(45),ah(46),ah(47),ah(48),ah(49),ah(50),ah(51),ah(52),
            ah(53),ah(54),ah(55),ah(56),ah(57),ah(58),ah(59),ah(60),ah(61),ah(62),ah(63),ah(64),ah(65),ah(66),ah(67),ah(68),ah(69),ah(70),ah(71),ah(72),ah(73),ah(74),ah(75),ah(76),ah(77),ah(78),
            ah(79),ah(80),ah(81),ah(82),ah(83),ah(84),ah(85),ah(86),ah(87),ah(88),ah(89),ah(90),ah(91),ah(92),ah(93),ah(94),ah(95),ah(96),ah(97),ah(98),ah(99),ah(100),ah(101),ah(102),ah(103),ah(104),
            ah(105),ah(106),ah(107),ah(108),ah(109),ah(110),ah(111),ah(112),ah(113),ah(114),ah(115),ah(116),ah(117),ah(118),ah(119),ah(120),ah(121),ah(122),ah(123),ah(124),ah(125),ah(126),ah(127),ah(128),ah(129),ah(130),
            ah(131),ah(132),ah(133),ah(134),ah(135),ah(136),ah(137),ah(138),ah(139),ah(140),ah(141),ah(142),ah(143),ah(144),ah(145),ah(146),ah(147),ah(148),ah(149),ah(150),ah(151),ah(152),ah(153),ah(154),ah(155),ah(156),
            ah(157),ah(158),ah(159),ah(160),ah(161),ah(162),ah(163),ah(164),ah(165),ah(166),ah(167),ah(168),ah(169),ah(170),ah(171),ah(172),ah(173),ah(174),ah(175),ah(176),ah(177),ah(178),ah(179),ah(180),ah(181),ah(182),
            ah(183),ah(184),ah(185),ah(186),ah(187),ah(188),ah(189),ah(190),ah(191),ah(192),ah(193),ah(194),ah(195),ah(196),ah(197),ah(198),ah(199),ah(200),ah(201),ah(202),ah(203),ah(204),ah(205),ah(206),ah(207),ah(208),
            ah(209),ah(210),ah(211),ah(212),ah(213),ah(214),ah(215),ah(216),ah(217),ah(218),ah(219),ah(220),ah(221),ah(222),ah(223),ah(224),ah(225),ah(226),ah(227),ah(228),ah(229),ah(230),ah(231),ah(232),ah(233),ah(234),
            ah(235),ah(236),ah(237),ah(238),ah(239),ah(240),ah(241),ah(242),ah(243),ah(244),ah(245),ah(246),ah(247),ah(248),ah(249),ah(250),ah(251),ah(252),ah(253),ah(254),ah(255),ah(256),ah(257),ah(258),ah(259),ah(260),
            ah(261),ah(262),ah(263),ah(264),ah(265),ah(266),ah(267),ah(268),ah(269),ah(270),ah(271),ah(272),ah(273),ah(274),ah(275),ah(276),ah(277),ah(278),ah(279),ah(280),ah(281),ah(282),ah(283),ah(284),ah(285),ah(286),
            ah(287),ah(288),ah(289),ah(290),ah(291),ah(292),ah(293),ah(294),ah(295),ah(296),ah(297),ah(298),ah(299),ah(300),ah(301),ah(302),ah(303),ah(304),ah(305),ah(306),ah(307),ah(308),ah(309),ah(310),ah(311),ah(312),
            ah(313),ah(314),ah(315),ah(316),ah(317),ah(318),ah(319),ah(320),ah(321),ah(322),ah(323),ah(324),ah(325),ah(326),ah(327),ah(328),ah(329),ah(330),ah(331),ah(332),ah(333),ah(334),ah(335),ah(336),ah(337),ah(338),
            ah(339),ah(340),ah(341),ah(342),ah(343),ah(344),ah(345),ah(346),ah(347),ah(348),ah(349),ah(350),ah(351),ah(352),ah(353),ah(354),ah(355),ah(356),ah(357),ah(358),ah(359),ah(360),ah(361),ah(362),ah(363),ah(364),
            ah(365),ah(366),ah(367),ah(368),ah(369),ah(370),ah(371),ah(372),ah(373),ah(374),ah(375),ah(376),ah(377),ah(378),ah(379),ah(380),ah(381),ah(382),ah(383),ah(384),ah(385),ah(386),ah(387),ah(388),ah(389),ah(390),
            ah(391),ah(392),ah(393),ah(394),ah(395),ah(396),ah(397),ah(398),ah(399),ah(400),ah(401),ah(402),ah(403),ah(404),ah(405),ah(406),ah(407),ah(408),ah(409),ah(410),ah(411),ah(412),ah(413),ah(414),ah(415),ah(416),
            ah(417),ah(418),ah(419),ah(420),ah(421),ah(422),ah(423),ah(424),ah(425),ah(426),ah(427),ah(428),ah(429),ah(430),ah(431),ah(432),ah(433),ah(434),ah(435),ah(436),ah(437),ah(438),ah(439),ah(440),ah(441),ah(442),
            ah(443),ah(444),ah(445),ah(446),ah(447),ah(448),ah(449),ah(450),ah(451),ah(452),ah(453),ah(454),ah(455),ah(456),ah(457),ah(458),ah(459),ah(460),ah(461),ah(462),ah(463),ah(464),ah(465),ah(466),ah(467),ah(468),
            ah(469),ah(470),ah(471),ah(472),ah(473),ah(474),ah(475),ah(476),ah(477),ah(478),ah(479),ah(480),ah(481),ah(482),ah(483),ah(484),ah(485),ah(486),ah(487),ah(488),ah(489),ah(490),ah(491),ah(492),ah(493),ah(494),
            ah(495),ah(496),ah(497),ah(498),ah(499),ah(500),ah(501),ah(502),ah(503),ah(504),ah(505),ah(506),ah(507),ah(508),ah(509),ah(510),ah(511),ah(512),ah(513),ah(514),ah(515),ah(516),ah(517),ah(518),ah(519),ah(520),
            ah(521),ah(522),ah(523),ah(524),ah(525),ah(526),ah(527),ah(528),ah(529),ah(530),ah(531),ah(532),ah(533),ah(534),ah(535),ah(536),ah(537),ah(538),ah(539),ah(540),ah(541),ah(542),ah(543),ah(544),ah(545),ah(546),
            ah(547),ah(548),ah(549),ah(550),ah(551),ah(552),ah(553),ah(554),ah(555),ah(556),ah(557),ah(558),ah(559),ah(560),ah(561),ah(562),ah(563),ah(564),ah(565),ah(566),ah(567),ah(568),ah(569),ah(570),ah(571),ah(572),
            ah(573),ah(574),ah(575),ah(576),ah(577),ah(578),ah(579),ah(580),ah(581),ah(582),ah(583),ah(584),ah(585),ah(586),ah(587),ah(588),ah(589),ah(590),ah(591),ah(592),ah(593),ah(594),ah(595),ah(596),ah(597),ah(598),
            ah(599),ah(600),ah(601),ah(602),ah(603),ah(604),ah(605),ah(606),ah(607),ah(608),ah(609),ah(610),ah(611),ah(612),ah(613),ah(614),ah(615),ah(616),ah(617),ah(618),ah(619),ah(620),ah(621),ah(622),ah(623),ah(624),
            ah(625),ah(626),ah(627),ah(628),ah(629),ah(630),ah(631),ah(632),ah(633),ah(634),ah(635),ah(636),ah(637),ah(638),ah(639),ah(640),ah(641),ah(642),ah(643),ah(644),ah(645),ah(646),ah(647),ah(648),ah(649),ah(650),
            ah(651),ah(652),ah(653),ah(654),ah(655),ah(656),ah(657),ah(658),ah(659),ah(660),ah(661),ah(662),ah(663),ah(664),ah(665),ah(666),ah(667),ah(668),ah(669),ah(670),ah(671),ah(672),ah(673),ah(674),ah(675),ah(676),
            ah(677),ah(678),ah(679),ah(680),ah(681),ah(682),ah(683),ah(684),ah(685),ah(686),ah(687),ah(688),ah(689),ah(690),ah(691),ah(692),ah(693),ah(694),ah(695),ah(696),ah(697),ah(698),ah(699),ah(700),ah(701),ah(702),
            ah(703),ah(704),ah(705),ah(706),ah(707),ah(708),ah(709),ah(710),ah(711),ah(712),ah(713),ah(714),ah(715),ah(716),ah(717),ah(718),ah(719),ah(720),ah(721),ah(722),ah(723),ah(724),ah(725),ah(726),ah(727),ah(728),
            ah(729),ah(730),ah(731),ah(732),ah(733),ah(734),ah(735),ah(736),ah(737),ah(738),ah(739),ah(740),ah(741),ah(742),ah(743),ah(744),ah(745),ah(746),ah(747),ah(748),ah(749),ah(750),ah(751),ah(752),ah(753),ah(754),
            ah(755),ah(756),ah(757),ah(758),ah(759),ah(760),ah(761),ah(762),ah(763),ah(764),ah(765),ah(766),ah(767),ah(768),ah(769),ah(770),ah(771),ah(772),ah(773),ah(774),ah(775),ah(776),ah(777),ah(778),ah(779),ah(780),
            ah(781),ah(782),ah(783),ah(784),ah(785),ah(786),ah(787),ah(788),ah(789),ah(790),ah(791),ah(792),ah(793),ah(794),ah(795),ah(796),ah(797),ah(798),ah(799),ah(800),ah(801),ah(802),ah(803),ah(804),ah(805),ah(806),
            matr_pro );
            
            ah:= ath_ass();
            ah.EXTEND(806);
         end if;
         --insert into asm_curve_ericsson_error values(r_curva.codice_allacciamento||';K-6;'||k||'rec-;'||rec||'I-;'||i);
        commit;
        if r_curva.cdc = 0 then 
        
        v_str_err := 'db1 POD SENZA CURVA Allaccio-utenza = '||
                                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza);
                      insert into asm_curve_ericsson_error values( v_str_err);
        --end if;
        --if length(trim(r_curva.cuc_curvav1))-length(trim(replace(r_curva.cuc_curvav1,';',null))) <97 then
        --v_str_err := 'Curva non Completa Allaccio-utenza = '||
                --                         trim(r_curva.codice_allacciamento)||'-'||trim(r_curva.progressivo_utenza);
                --      insert into asm_curve_ericsson_error values( v_str_err);
        commit;
        goto fine_rec_db1; 
        end if;
        
        
       
      
    end loop;  
    <<FINE_REC_db1>>
      
       
    commit;
   end loop;
     <<fine_loop_db1>> 
    COMMIT;
     if v_errore = 1
     then 
     
       insert into asm_curve_ericsson_error values( v_str_err);
       COMMIT;
       v_errore:=0;
     end if; 
      commit;
      end;